package com.spark.usermusicService.service.impl;

import com.spark.R;
import com.spark.usermusicService.entity.User;
import com.spark.usermusicService.service.RegisterService;
import com.spark.usermusicService.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhouqichun
 * @version 1.0
 * @date 2021/7/1 - 07 - 01 - 15:48
 * @description 注册Service层实现类
 */
@Service
public class RegisterServiceImpl implements RegisterService {
    //注入UserService
    @Autowired
    private UserService userService;

    @Override
    public R register(User user){
        //map用来存储注册过程中的相关信息
        Map<String, Object> map = new HashMap<>(2);
        //账户是否存在相关信息
        map.put("isUserExist", "No");
        //昵称是否存在相关信息
        map.put("isNickNameExist", "No");
        R r = R.ok();
        r.setMessage("注册成功");

        //判断账户是否已经存在用户表中
        if(userService.getUserById(user.getId()) != null){
            map.put("isUserExist", "Yes");
            r = R.error();
            r.setMessage("注册失败");
        }

        //判断昵称是否已经被其他用户使用，因为昵称是平台唯一性的
        if (userService.getUserByNickName(user.getNickname())!=null){
            map.put("isNickNameExist", "Yes");
            r = R.error();
            r.setMessage("注册失败");
        }

        //注册成功，向用户表中插入数据
        if(r.getSuccess()){
            userService.insertUser(user);
        }
        return r.data(map);
    }
}
